﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Reflection;
using giaoxuhaison.Common;

namespace giaoxuhaison.Common
{
    public class EntityHelper : IEntityHelper
    {
        public int GetIdValue<T>(T obj)
        {
            return (int)obj.GetType().GetProperty("Id").GetValue(obj, null);
        }

        public int GetKeyValueOfBusinessEntity<T>(T obj)
        {
            return (int)obj.GetType().GetProperty("Key").GetValue(obj, null);
        }

        public string GetPropertyNameOfEntityByDisplayName<T>(string displayName)
        {
            var property = GetPropertyOfEntityByDisplayName<T>(displayName);
            if (property != null)
            {
                return property.Name;
            }

            return string.Empty;
        }

        public string GetPropertyNameOfEntityByDisplayName<T>(string displayName, T source)
        {
            var property = GetPropertyOfEntityByDisplayName<T>(displayName, source);
            if (property != null)
            {
                return property.Name;
            }

            return string.Empty;
        }

        public PropertyInfo GetPropertyOfEntityByDisplayName<T>(string displayName)
        {
            var propertyList = typeof(T).GetProperties();
            foreach (var item in propertyList)
            {
                if (item.Name == displayName)
                {
                    return item;
                }

                var customAttrList = item.GetCustomAttributes(false);
                var customAttr = customAttrList.Where(x => x.GetType() == typeof(DisplayNameAttribute))
                    .Select(x => (DisplayNameAttribute)x)
                    .FirstOrDefault(x => x.DisplayName == displayName);
                if (customAttr != null)
                {
                    return item;
                }
            }

            return null;
        }

        public PropertyInfo GetPropertyOfEntityByDisplayName<T>(string displayName, T source)
        {
            var propertyList = source.GetType().GetProperties();
            foreach (var item in propertyList)
            {
                if (item.Name == displayName)
                {
                    return item;
                }

                var customAttrList = item.GetCustomAttributes(false);
                var customAttr = customAttrList.Where(x => x.GetType() == typeof(DisplayNameAttribute))
                    .Select(x => (DisplayNameAttribute)x)
                    .FirstOrDefault(x => x.DisplayName == displayName);
                if (customAttr != null)
                {
                    return item;
                }
            }

            return null;
        }

        public Type GetPropertyType(Type modelType, string propertyName)
        {
            return modelType.GetProperty(propertyName).PropertyType;
        }
    }
}